package com.arloesol.android.jimmobileblue;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.widget.RemoteViews;

public class WidgetProvider1x1 extends AppWidgetProvider {

  @Override
  public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    if (intent.getAction().equals("update_jimBLUE_balances_screen")) {

      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
      ComponentName thisAppWidget = new ComponentName(context.getPackageName(), WidgetProvider1x1.class.getName());
      int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);

      onUpdate(context, appWidgetManager, appWidgetIds);
    }
  }

  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < appWidgetIds.length; i++) {
      int appWidgetId = appWidgetIds[i];

      // Get the layout for the App Widget
      RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.widget1x1_dark);

      /*
       * // Create an Intent to refresh the balances Intent intentRefresh = new Intent(context, MainService.class);
       * intentRefresh.setAction("com.beek.android.jim.sendsms"); intentRefresh.putExtra("Type", "");
       * intentRefresh.putExtra("Message", "CONSULT"); PendingIntent pendingIntentRefresh =
       * PendingIntent.getService(context, 0, intentRefresh, 0); // attach an on-click listener to the button to launch
       * a balance // refresh view.setOnClickPendingIntent(R.id.widget_layout1x1, pendingIntentRefresh);
       */

      // Create an Intent to launch the JimBLUE app
      Intent intentSMS = new Intent(context, MainActivity.class);
      PendingIntent pendingIntentSMS = PendingIntent.getActivity(context, 0, intentSMS, 0);
      // attach an on-click listener to the textfield
      view.setOnClickPendingIntent(R.id.widget_layout1x1, pendingIntentSMS);

      setTexts(context, view);

      // Tell the AppWidgetManager to perform an update on the current app
      // widget
      appWidgetManager.updateAppWidget(appWidgetId, view);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
  }
  
  private void setTexts(Context context, RemoteViews view) {
    SetTextAndColor(context, view, R.id.call_text, "all");
    SetTextAndColor(context, view, R.id.sms_text, "allsms");
    SetTextAndColor(context, view, R.id.data_text, "data");
  }

  private void SetTextAndColor(Context context, RemoteViews view, int id, String balancetype) {
    String text;
    int days, color = 1;
    SharedPreferences balances = context.getSharedPreferences("balances", 0);

    text = balances.getString(balancetype.concat("_value"), "0");

    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    String dateTo = balances.getString(balancetype.concat("_dateTo"), "");
    try {
      Date dateToDate = df.parse(dateTo);
      days = (int) ((dateToDate.getTime() - Calendar.getInstance().getTimeInMillis()) / (1000 * 60 * 60 * 24));
    } catch (ParseException e) {
      days = 365; // if no expiration - consider days is big
    }
    if (days > 6) {
      color = Color.WHITE;
    } else if (days > 4) {
      color = Color.YELLOW;
    } else if (days > 2) {
      color = Color.rgb(255, 165, 79); // light orange
    } else if (days > 0) {
      color = Color.rgb(255, 90, 40); // orange
    } else {
      color = Color.RED;
    }

    view.setTextViewText(id, text);
    view.setTextColor(id, color);

  }

}
